package com.chen.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.entity.ExamChoose;
import com.chen.mapper.ExamChooseMapper;
import com.chen.service.ExamChooseService;
import com.chen.vo.ExamChooseVO;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * 考试中已选择的记录
 */
@Service
public class ExamChooseServiceImpl extends ServiceImpl<ExamChooseMapper, ExamChoose> implements ExamChooseService {

    /**
     * 回显学生已填写答案
     *
     * @param studentId
     * @param examPaperId
     * @return
     */
    @Override
    public List<ExamChooseVO> getChooseInfoWithSumScore(Integer studentId, Integer examPaperId) {
        if (ObjectUtil.isNotEmpty(studentId) && ObjectUtil.isNotEmpty(examPaperId)) {
            return baseMapper.findChooseByStudentIdAndExamPaperId(studentId, examPaperId);
        }
        return Collections.emptyList();
    }

    /**
     * 记录答题
     * @param studentId
     * @param examPaperId
     * @param subjectId
     * @param chooseAswer 回答的答案
     * @return
     */
    @Override
    public void recordAnswer(Integer studentId, Integer examPaperId, Integer subjectId, String chooseAswer) {
        if (ObjectUtil.isNotEmpty(studentId) && ObjectUtil.isNotEmpty(examPaperId) && ObjectUtil.isNotEmpty(subjectId) && ObjectUtil.isNotEmpty(chooseAswer)) {
            ExamChoose chooseWithIds = baseMapper.findChooseByStudentIdAndExamPaperIdAndSubjectId(studentId, examPaperId, subjectId);
            if (ObjectUtil.isNotEmpty(chooseWithIds)) {
                chooseWithIds.setResult(chooseAswer);
                baseMapper.updateById(chooseWithIds);
            }else {
                ExamChoose examChoose = new ExamChoose();
                examChoose.setStudentId(studentId);
                examChoose.setExamPaperId(examPaperId);
                examChoose.setSubjectId(subjectId);
                examChoose.setResult(chooseAswer);
                baseMapper.insert(examChoose);
            }
        }
    }
}
